﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace IntelligentAgriculture.Domain.Impl
{
    // 仓储类实现
    public class HistoryRecordRepository : IHistoryRecordRepository
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly AgricultureDbContext _dbContext;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext">上下文</param>
        public HistoryRecordRepository(AgricultureDbContext dbContext)
        {
            _dbContext = dbContext;
        }


        /// <summary>
        /// 历史记录显示
        /// </summary>
        /// <returns>返回数据</returns>
        public async Task<List<Historyrecord>> GetHistoryRecordsAsync()
        {
            return _dbContext.Historyrecords.Where(c => c.Isdel == false).ToList();
        }





        /// <summary>
        /// 逻辑删除历史记录表
        /// </summary>
        /// <param name="hrid">主键id</param>
        /// <returns>受影响的行数</returns>
        public Task<int> DeleteHistoryrecord(int hrid)
        {
            var list = _dbContext.Historyrecords.FirstOrDefault(c => c.Hrid == hrid);
            if (list != null)
            {
                list.Isdel = true;
                _dbContext.Historyrecords.Update(list);
            }
            return _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 批量逻辑删历史记录表
        /// </summary>
        /// <param name="hrids">id</param>
        /// <returns>返回数据</returns>
        public Task<int> DeleteRangeHistoryrecord(IEnumerable<int> hrids)
        {
            foreach (int item in hrids)
            {
                var list = _dbContext.Historyrecords.FirstOrDefault(c => c.Hrid == item);
                if (list != null)
                {
                    list.Isdel = true; // 标记为逻辑删除
                    _dbContext.Historyrecords.Update(list);
                }
            }
            return _dbContext.SaveChangesAsync();
        }



        /// <summary>
        /// 添加种母历史记录数据
        /// </summary>
        /// <param name="inputs">实体</param>
        /// <returns>返回受影响的行数</returns>
        public Task<int> AddHistoryrecordAsync(List<Historyrecord> inputs)
        {

            _dbContext.Historyrecords.AddRangeAsync(inputs);

            return _dbContext.SaveChangesAsync(); // 异步保存所有更改到数据库
        }



        /// <summary>
        /// 反填种母历史记录表
        /// </summary>
        /// <param name="earTag">根据耳号反填</param>
        /// <returns>返回数据</returns>
        public async Task<Historyrecord> GetByIdAsync(string? earTag)
        {

            var list = _dbContext.Historyrecords.FirstOrDefault(c => c.EarTag == earTag);

            return list;
        }
        /// <summary>
        /// 修改种母历史记录表
        /// </summary>
        /// <param name="input">实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> UpdateHisRecordAsync(Historyrecord input)
        {
            _dbContext.Historyrecords.Update(input);
            return await _dbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 批量修改种母历史记录表
        /// </summary>
        /// <param name="input">实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> ListUpdateHisRecordAsync(List<Historyrecord> input)
        {
            _dbContext.Historyrecords.UpdateRange(input);
            return await _dbContext.SaveChangesAsync();
        }








    }
}
